package net.seehealth.healthpad.application.service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.seehealth.healthpad.Constants;
import net.seehealth.healthpad.business.foodmanage.FoodManageLogic;
import net.seehealth.healthpad.business.foodmanage.service.IFoodManageLogic;
import net.seehealth.healthpad.business.sportmanage.SportManageLogic;
import net.seehealth.healthpad.business.sportmanage.service.ISportManageLogic;
import net.seehealth.healthpad.model.foodmanage.Food;
import net.seehealth.healthpad.model.foodmanage.FoodGuide;
import net.seehealth.healthpad.model.foodmanage.FoodType;
import net.seehealth.healthpad.model.sportmanage.SportGuide;
import net.seehealth.healthpad.model.sportmanage.SportItem;
import net.seehealth.healthpad.model.sportmanage.SportType;
import net.seehealth.healthpad.model.usermanage.UserInfo;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Xml;

public class HealthRemoteService {
	private IFoodManageLogic iFoodManageLogic;
	private ISportManageLogic iSportManageLogic;
	public HealthRemoteService(Context context) {
		iFoodManageLogic = new FoodManageLogic(context);
		iSportManageLogic = new SportManageLogic(context);
	}
	/**
	 * 通过userID验证登陆
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:00
	 */
	public boolean validateRemoteUserInfo(UserInfo userInfo) {
		// 调用webservice接口获取到xml数据
		boolean bool  = false;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		soapObject.addProperty("userInfo", userInfo);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				if(xmlStr.equals("true")){
					bool = true;
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bool;
	}
	/**
	 * 通过userID远程获取用户基本信息
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:00
	 */
	public UserInfo getRemoteUserInfo(int userID) {
		// 调用webservice接口获取到xml数据
		UserInfo userInfo = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		soapObject.addProperty("userID", userID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				userInfo = parseXmlAndSaveUserInfo(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userInfo;
	}
	/**
	 * 解析家庭成员数据
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:48:25
	 */
	public UserInfo parseXmlAndSaveUserInfo(String xmlStr) {
		UserInfo userInfo = new UserInfo();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					// persons = new ArrayList<Person>();
					break;

				case XmlPullParser.START_TAG:
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(eventType==0){
							break;
						}
					}
					if (userInfo != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("UserID".equals(name)) {
							userInfo.setUserID(new Integer(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("Name".equals(name)) {
							userInfo
									.setName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("UserName".equals(name)) {
							userInfo
									.setUserName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("Sex".equals(name)) {
							userInfo.setSex(parser
									.nextText());
						}
						if ("PetName".equals(name)) {
							userInfo.setPetName(parser
									.nextText());
						}
						if ("CardNO".equals(name)) {
							userInfo.setCardNo(parser.nextText());
						}
						if ("MobileNO".equals(name)) {
							userInfo.setMobileNo(parser.nextText());
						}
						if ("HomeNO".equals(name)) {
							userInfo.setHomeNo(parser.nextText());
						}
						if ("Address".equals(name)) {
							userInfo.setAddress(parser.nextText());
						}
						if ("SOSName".equals(name)) {
							userInfo.setSosName(parser.nextText());
						}
						if ("SOSconstacts".equals(name)) {
							userInfo.setSosConstact(parser.nextText());
						}
						if ("FamialyNO".equals(name)) {
							userInfo.setFamialyNo(parser.nextText());
						}
						if ("Password".equals(name)) {
							userInfo.setPassword(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("person".equals(parser.getName())) {
						// persons.add(person);
						// person = null;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//FoodGuide tmepFoodGuide = iFoodManageLogic.saveFoodGuide(foodGuide)
		try {
			inStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userInfo;
	}

	/**
	 * 通过userID远程获取运动指导数据
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:37
	 */
	public SportGuide getRemoteSportGuide(int userID) {
		// 调用webservice接口获取到xml数据
		SportGuide sportGuide = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		soapObject.addProperty("userID", userID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				sportGuide = parseXmlAndSaveSportGuide(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sportGuide;
	}
	/**
	 * 解析运动指导数据
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:47:45
	 */
	private SportGuide parseXmlAndSaveSportGuide(String xmlStr) {
		SportGuide sportGuide = null;
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					// persons = new ArrayList<Person>();
					break;

				case XmlPullParser.START_TAG:
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(eventType==0){
							break;
						}
					}
					if (sportGuide != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("UserID".equals(name)) {
							sportGuide.setUserID(new Integer(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("bmi".equals(name)) {
							sportGuide
									.setBmi(new Float(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("nowWeight".equals(name)) {
							sportGuide
									.setNowWeight(new Float(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("ideaWeight".equals(name)) {
							sportGuide.setIdeaWeight(new Float(parser
									.nextText()));
						}
						if ("todaySport".equals(name)) {
							sportGuide.setTodaySport(parser
									.nextText());
						}
						if ("sportAdvice".equals(name)) {
							sportGuide.setSportAdvice(parser.nextText());
						}
						if ("sportTaboo".equals(name)) {
							sportGuide.setSportTaboo(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("person".equals(parser.getName())) {
						// persons.add(person);
						// person = null;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//FoodGuide tmepFoodGuide = iFoodManageLogic.saveFoodGuide(foodGuide)
		return null;
	}

	/**
	 * 通过userID远程获取膳食指导数据
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:43
	 */
	public FoodGuide getRemoteFoodGuide(int userID) {
		FoodGuide foodGuide = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		soapObject.addProperty("userID", userID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				foodGuide = parseXmlAndSaveFoodGuide(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foodGuide;
	}
	/**
	 * 解析膳食指导数据并保存
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:30:29
	 */
	private FoodGuide parseXmlAndSaveFoodGuide(String xmlStr) {
		FoodGuide foodGuide = new FoodGuide();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					// persons = new ArrayList<Person>();
					break;

				case XmlPullParser.START_TAG:
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(eventType==0){
							break;
						}
					}
					if (foodGuide != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("UserID".equals(name)) {
							foodGuide.setUserID(new Integer(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("nowWeight".equals(name)) {
							foodGuide
									.setNowWeight(new Float(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("ideaWeight".equals(name)) {
							foodGuide
									.setIdeaWeight(new Float(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("standardIntake".equals(name)) {
							foodGuide.setStandardIntake(new Float(parser
									.nextText()));
						}
						if ("todayIntake".equals(name)) {
							foodGuide.setTodayIntake(new Float(parser
									.nextText()));
						}
						if ("foodName".equals(name)) {
							foodGuide.setFoodName(parser.nextText());
						}
						if ("practice".equals(name)) {
							foodGuide.setPractice(parser.nextText());
						}
						if ("png".equals(name)) {
							foodGuide.setPng(parser.nextText());
						}
						if ("foodAdvice".equals(name)) {
							foodGuide.setFoodAdvice(parser.nextText());
						}
						if ("foodTaboo".equals(name)) {
							foodGuide.setFoodTaboo(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("person".equals(parser.getName())) {
						// persons.add(person);
						// person = null;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//FoodGuide tmepFoodGuide = iFoodManageLogic.saveFoodGuide(foodGuide);
		return foodGuide;
	}
	/**
	 * 远程获取所有食材
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:43
	 */
	public List<Food> getRemoteFoods() {

		List<Food> foods = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				foods = parseXmlAndSaveFoods(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foods;
	}
	/**
	 * 解析食材数据并保存食材
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:31:56
	 */
	private List<Food> parseXmlAndSaveFoods(String xmlStr) {
		List<Food> foods = new ArrayList<Food>();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			int type  = 0;
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				Food food = new Food(); 
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(type==0){
							break;
						}
					}
					if (food != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("code".equals(name)) {
							food.setCode(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("name".equals(name)) {
							food.setName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("kc".equals(name)) {
							food.setKc(new Float(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("desc".equals(name)) {
							food.setDesc(parser
									.nextText());
						}
						if ("typeCode".equals(name)) {
							food.setTypeCode(parser
									.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("style".equals(parser.getName())) {
						//保存食材
						//Food tmepFood = iFoodManageLogic.saveFood(food);
						foods.add(food);
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return foods;
	}
	/**
	 * 远程获取所有运动项目
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:43
	 */
	public List<SportItem> getRemoteSportItems() {

		List<SportItem> sportItems = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				sportItems = parseXmlAndSaveSportItems(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sportItems;
	}
	/**
	 * 解析运动项目数据并保存运动项目
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:31:56
	 */
	private List<SportItem> parseXmlAndSaveSportItems(String xmlStr) {
		List<SportItem> sportItems = new ArrayList<SportItem>();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			int type  = 0;
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				SportItem sportItem = new SportItem(); 
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(type==0){
							break;
						}
					}
					if (sportItem != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("sportItemNo".equals(name)) {
							sportItem.setSportItemNo(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("sportTypeCode".equals(name)) {
							sportItem.setSportTypeCode(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("sportIntension".equals(name)) {
							sportItem.setSportIntension(new Integer(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("sportName".equals(name)) {
							sportItem.setSportName(parser
									.nextText());
						}
						if ("metValue".equals(name)) {
							sportItem.setMetValue(new Float(parser
									.nextText()));
						}
						if ("sportNum".equals(name)) {
							sportItem.setSportNum(new Float(parser
									.nextText()));
						}
						if ("sportTime".equals(name)) {
							sportItem.setSportTime(new Integer(parser
									.nextText()));
						}
						if ("png".equals(name)) {
							sportItem.setPng(parser
									.nextText());
						}
						if ("sportAdvice".equals(name)) {
							sportItem.setSportAdvice(parser
									.nextText());
						}
						if ("desc".equals(name)) {
							sportItem.setDesc(parser
									.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("style".equals(parser.getName())) {
						//保存运动项目
						boolean bool = iSportManageLogic.saveLocalSportItem(sportItem);
						sportItems.add(sportItem);
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return sportItems;
	}
	
	/**
	 * 远程获取所有运动类型
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:43
	 */
	public List<SportType> getRemoteSportTypes() {

		List<SportType> sportTypes = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				sportTypes = parseXmlAndSaveSportTypes(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sportTypes;
	}

	/**
	 * 解析运动类型数据并保存运动类型
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:31:56
	 */
	private  List<SportType> parseXmlAndSaveSportTypes(String xmlStr) {
		List<SportType> sportTypes = new ArrayList<SportType>();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			int type  = 0;
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				SportType sportType = new SportType(); 
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(type==0){
							break;
						}
					}
					if (sportType != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("sportTypeCode".equals(name)) {
							sportType.setSportTypeCode(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("typeName".equals(name)) {
							sportType.setTypeName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("style".equals(parser.getName())) {
						//保存食材
						boolean bool  = iSportManageLogic.saveLocalSportType(sportType);
						sportTypes.add(sportType);
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return sportTypes;
	}
	/**
	 * 远程获取所有食材类型
	 * 
	 * @description:TODO
	 * @param
	 * @return
	 * @throws Exception
	 * @author TODO
	 * @date 2012-4-12 下午3:29:43
	 */
	public List<FoodType> getRemoteFoodTypes() {

		List<FoodType> foodTypes = null;
		String xmlStr = null;
		SoapObject soapObject = new SoapObject(
				Constants.Common.XMLTARGETNAMESPACE,
				Constants.Common.GETFOODSBYUSERID);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransport = new HttpTransportSE(
				Constants.Common.WSDL);
		try {
			httpTransport.call(Constants.Common.XMLTARGETNAMESPACE
					+ Constants.Common.GETFOODSBYUSERID, envelope);
			SoapObject result = (SoapObject) envelope.getResponse();
			int count = result.getPropertyCount();
			if (count != 0) {
				xmlStr = (String) result.getProperty(0);
				// 解析数据并保存到数据库
				foodTypes = parseXmlAndSaveFoodTypes(xmlStr);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foodTypes;
	}

	/**
	 * 解析食材类型数据并保存食材类型
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 下午5:31:56
	 */
	private List<FoodType> parseXmlAndSaveFoodTypes(String xmlStr) {
		List<FoodType> foodTypes = new ArrayList<FoodType>();
		ByteArrayInputStream inStream = new ByteArrayInputStream(
				xmlStr.getBytes());
		// 解析本地文件
		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();// 产生第一个事件
			int type  = 0;
			while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
				FoodType foodType = new FoodType(); 
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					
					String name = parser.getName();// 获取解析器当前指向的元素的名称
					if ("style".equals(name)) {
						if(type==0){
							break;
						}
					}
					if (foodType != null) {
						if ("isOrNo".equals(name)) {
							Integer isOrNo = Integer.valueOf(parser.nextText());
							if(isOrNo==0){
								return null;
							}
						}
						if ("foodTypeCode".equals(name)) {
							foodType.setFoodTypeCode(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("foodTypeName".equals(name)) {
							foodType.setFoodTypeName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
						}
						if ("parentID".equals(name)) {
							foodType.setParentID(new Integer(parser.nextText()));// 获取解析器当前指向元素的下一个文本节点的值
						}
					}
					break;

				case XmlPullParser.END_TAG:
					if ("style".equals(parser.getName())) {
						//保存食材
						//FoodType tempfoodType = iFoodManageLogic.saveFoodType(foodType);
						foodTypes.add(foodType);
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return foodTypes;
	}
}
